package com.aaa.outbound.controller;


import com.aaa.entity.outbound.Outorder;
import com.aaa.entity.outbound.OutorderDetail;
import com.aaa.entity.stock.Stock;
import com.aaa.outbound.feign.StockFeign;
import com.aaa.outbound.service.OutorderDetailService;
import com.aaa.outbound.service.OutorderService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/outbound/outorder-detail")
public class OutorderDetailController {
    @Autowired
    private OutorderDetailService outorderDetailService;
    @Autowired
    private OutorderService outorderService;
    @Autowired
    private StockFeign stockFeign;


    @PostMapping("/searchOutOrderDetail/{outorderno}")
    @ApiOperation("分页查询")
    public R searchOutOrderDetail(@ApiParam("出库订单编号")@PathVariable String outorderno){
        List<OutorderDetail> outorderDetails = outorderDetailService.selectOutoderDetailByOutorderno(outorderno);
        return R.ok().data("outorderDetail",outorderDetails).code(200);
    }

    @PostMapping("addOutOrderDetail/{outorderno}")
    @ApiOperation(value = "录入订单详情")
    public R addOutOrderDetail(@ApiParam(value = "订单编号") @PathVariable("outorderno") String outorderno ,@ApiParam(value = "传入包装类") @RequestBody OutorderDetail outorderDetail){
        if(outorderDetail.getCommoditybar()==null){
            return R.error().message("请添加商品");
        }
        if(outorderDetail.getQty()==null){
            return R.error().message("商品数量不能为空");
        }
        if (outorderDetail.getQty()==0){
            return R.error().message("商品数量不能为零");
        }
        if(outorderDetail.getQty()>1000){
            return R.error().message("单次录入商品数量不能大于1000");
        }
        String commoditybar = outorderDetail.getCommoditybar();
        QueryWrapper<OutorderDetail> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("commoditybar", commoditybar);
        queryWrapper1.eq("outorderno", outorderno);
        List<OutorderDetail> list = outorderDetailService.list(queryWrapper1);
        if(list.size()!=0){
            Date now=new Date();
            Long id = list.get(0).getId();
            outorderDetail.setId(id);
            outorderDetail.setOutorderno(outorderno);
            outorderDetail.setQty(outorderDetail.getQty()+list.get(0).getQty());
            outorderDetail.setUpdatetime(now);
            outorderDetailService.updateById(outorderDetail);
        }else {
            Date now=new Date();
            outorderDetail.setOutorderno(outorderno);
            outorderDetail.setCreatetime(now);
            List<Stock> stocks = stockFeign.selectStockByCommoditybar(commoditybar, 41L);
            String commodityname = stocks.get(0).getCommodityname();
            Long commodityid = stocks.get(0).getCommodityid();
            outorderDetail.setCommodityid(commodityid);
            outorderDetail.setCommodityname(commodityname);
            outorderDetailService.save(outorderDetail);
        }
        QueryWrapper<OutorderDetail> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("outorderno",outorderno);
        List<OutorderDetail> list1 = outorderDetailService.list(queryWrapper);
        Integer sum = outorderDetailService.selectSumQtyByOutorderno(outorderno);
        Outorder outorder1 = outorderService.selectIdByOutorderno(outorderno);
        outorder1.setQty(sum);
        outorder1.setCategory(list1.size());
        outorderService.updateById(outorder1);
        return R.ok().message("录入成功").code(200);
    }

    @PostMapping("uncheckOutOrder/{outorderno}")
    @ApiOperation(value = "结束录入出库订单")
    public R uncheckOutOrder(@ApiParam(value = "出库订单编号") @PathVariable("outorderno") String outorderno){
        List<OutorderDetail> outorderDetails = outorderDetailService.selectOutoderDetailByOutorderno(outorderno);
        if(outorderDetails.size()!=0) {
            QueryWrapper<Outorder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("outorderno", outorderno);
            Outorder outorder = outorderService.getOne(queryWrapper);
            outorder.setState("1");
            outorderService.updateById(outorder);
            return R.ok().message("录入成功").code(200);
        }else {
            return R.error().message("未录入明细");
        }
    }
}

